package com.xjtu.chapter03;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;


/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/11 22:17
 */
public class Ex3_TimeClassTest {
    //测试java中日期和时间类的综合使用
    public static void main(String[] args) {

        //测试Date
        Date d = new Date();
        System.out.println("现在的时间为："+d);

        //测试Calendar
        Calendar c = Calendar.getInstance();
        System.out.println("今天是："+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
        System.out.println("今天在今年中是第 "+c.get(Calendar.DAY_OF_YEAR)+" 天");

        //测试GregorianCalendar
        GregorianCalendar ca = new GregorianCalendar();
        System.out.println("公元时间是："+ca.getTime());

        //SimpleDateFormat的用法
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println("格式化时间："+myFormat.format(new Date()));

        //测试clock
        final Clock clock = Clock.systemUTC();
        System.out.println("Clock获得的当前时间为："+clock.instant());

        //测试Localdate和Localtime
        final LocalDate date  = LocalDate.now();
        final LocalDate dateFromClock = LocalDate.now(clock);
        System.out.println("LocalDate获取当前日期："+date);
        System.out.println("LocalDate从Clock获得的当前时间："+dateFromClock);

        final LocalTime time = LocalTime.now();
        final LocalTime timeFromClock = LocalTime.now(clock);
        System.out.println("LocalTime获取当前日期："+time);
        System.out.println("LocalTime从Clock获得的当前时间："+timeFromClock);

        //测试LocalDateTime
        final LocalDateTime datetime = LocalDateTime.now();
        final LocalDateTime datetimeFromClock = LocalDateTime.now(clock);
        System.out.println("LocalDateTime获取当前日期："+datetime);
        System.out.println("LocalDateTime从Clock获得的当前时间："+datetimeFromClock);

        //测试zonedDateTime
        final ZonedDateTime zonedDateTime = ZonedDateTime.now();
        final ZonedDateTime zonedDateTimeFromClock = ZonedDateTime.now(clock);
        final ZonedDateTime zonedDateTimeFromeZone = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
        System.out.println("当前时区时间："+zonedDateTime);
        System.out.println("从Clock获得的当前时区时间："+zonedDateTimeFromClock);
        System.out.println("美国洛杉矶时间："+zonedDateTimeFromeZone);
    }
}
